#include "${LavishScript.CurrentDirectory}/Scripts/ezvg/includes.iss"
variable string UIFile = "${LavishScript.CurrentDirectory}/Scripts/ezvg/UI/UI.xml"
variable string UISkin = "${LavishScript.CurrentDirectory}/Interface/VGSkin.xml"
variable string scripName = "ezvg"

function main()
{
	ext -require isxvg
	wait 100 ${ISXVG.IsReady}
	if !${ISXVG.IsReady}
	{
		echo "Unable to load ISXVG, exiting script"
		endscript ${scripName}
	}
  wait 30
  call xmlPrep
	
	ui -reload "${UISkin}"
	ui -reload -skin VGSkin "${UIFile}"
  
	;ui -load -skin VGSkin "${UIFile}"
  UIElement[EZVGCommands]:Hide

  call PopulateUI

  Event[OnFrame]:AttachAtom[LogicPulse]
	Event[VG_onPawnStatusChange]:AttachAtom[VG_onPawnStatusChange]
	Event[VG_onGroupMemberDeath]:AttachAtom[VG_onGroupMemberDeath]
  Event[VG_OnIncomingCombatText]:AttachAtom[VG_OnIncomingCombatText]
	Event[VG_OnIncomingText]:AttachAtom[VG_OnIncomingText]
	Event[VG_onAlertText]:AttachAtom[VG_onAlertText]
  Event[VG_onAddInventoryItem]:AttachAtom[VG_onAddInventoryItem]
  Event[VG_onConnectionStateChange]:AttachAtom[VG_onConnectionStateChange]
  Event[VG_onChangeChunk]:AttachAtom[VG_onChangeChunk]
  
  call AllAbilCombos

  echo ${scripName} is R-E-A-D-Y ... please enjoy carefully.
  do
  {
    ;if (${Me.ToPawn.IsDead} && ${Me.Health}==0 ) || ${Paused}
    ;{
    ;  waitframe
    ;}
  }
  while ${isRunning}
}

function atexit()
{
	call xmlSave
  Event[OnFrame]:DetachAtom[LogicPulse]
	Event[VG_onPawnStatusChange]:DetachAtom[VG_onPawnStatusChange]
	Event[VG_onGroupMemberDeath]:DetachAtom[VG_onGroupMemberDeath]
  Event[VG_OnIncomingCombatText]:DetachAtom[VG_OnIncomingCombatText]
  Event[VG_onAlertText]:DetachAtom[VG_onAlertText]
  Event[VG_onAddInventoryItem]:DetachAtom[VG_onAddInventoryItem]
  Event[VG_onConnectionStateChange]:DetachAtom[VG_onConnectionStateChange]
  Event[VG_onChangeChunk]:DetachAtom[VG_onChangeChunk]
	ui -unload "${UIFile}"
	ui -unload "${UISkin}"
	echo ${scripName} script is now ending.
	if (!${ISXVG(exists)})
	{
		return
	}
}

atom(script) LogicPulse()
{
  if ${Script.RunningTime}>${Mov.nextCheckT}
    Mov:LogicPulse
  if ${Script.RunningTime}>${NonCmbt.nextCheckT}
    NonCmbt:LogicPulse
  if ${Script.RunningTime}>${Combat.nextCheckT}
    Combat:LogicPulse
  if ${Script.RunningTime}>${VerifyIDCheckT}
    VerifyID
}

atom VG_onChangeChunk(string NewChunkX, string NewChunkY)
{
  Script[ezvg]:ExecuteAtom[HaxPortList]
}

atom(script) EZVGCommands(string command)
{
  echo ezvgcommands atom ${command}
  if ( ${command.Find[PAUSE]} && !${Paused} ) || ( ${command.Find[RUN]} && ${Paused} )
    UIElement[ezvg].FindUsableChild[Run Button,button]:LeftClick
  elseif ${command.Find[ASSIST]}
    Combat:ForceAssist
  elseif ${command.Find[LOG OUT]}
  {
    if !${Paused}
      UIElement[ezvg].FindUsableChild[Run Button,button]:LeftClick
    VGExecute /camp
  }
}

atom(script) EZVGListAmends(string list, string name1, string name2)
{
  if ${list.Find[HealsTo]}
  {
    if !${LavishSettings[MobDict].FindSet[${name1}](exists)}
      LavishSettings:AddSet[${name1}]
    if ${LavishSettings[MobDict].FindSet[${name1}].FindSetting[HealsTo](exists)}
      LavishSettings[MobDict].FindSet[${name1}].FindSetting[HealsTo]:Remove
    LavishSettings[MobDict].FindSet[${name1}]:AddSetting[HealsTo, ${name2}]
    call xmlSave
    call xmlLoad
  }
}

atom VG_onConnectionStateChange(string NewConnectionState)
{
  echo ### ${NewConnectionState}
}

atom(script) VerifyID()
{
  ; necessary for the times when the server reassigns IDs upon death and chunking
  ; when the rest of the bots are NOT around or in range.
  if ${Assist.Length}>0 && !${Pawn[ID, ${AssistID}](exists)}
  {
    if ${Pawn[Name, ${Assist}](exists)}
    {
      echo @#- VerifyID atom -#@ resetting AssistID ${AssistID} with new ID ${Pawn[Name, ${Assist}].ID}
      AssistID:Set[${Pawn[Name, ${Assist}].ID}]
    }
  }
  if ${Tank.Length}>0 && !${Pawn[ID, ${TankID}](exists)}
  {
    if ${Pawn[Name, ${Tank}](exists)}
    {
      echo @#- VerifyID atom -#@ resetting TankID ${TankID} with new ID ${Pawn[Name, ${Tank}].ID}
      TankID:Set[${Pawn[Name, ${Tank}].ID}]
    }
  }
  if ${Position.Length}>0 && !${Pawn[ID, ${PositionID}](exists)}
  {
    if ${Pawn[Name, ${Position}](exists)}
    {
      echo @#- VerifyID atom -#@ resetting PositionID ${PositionID} with new ID ${Pawn[Name, ${Position}].ID}
      PositionID:Set[${Pawn[Name, ${Position}].ID}]
    }
  }
  if ${DTgtDeaggro.Length}>0 && !${Pawn[ID, ${DTgtDeaggroID}](exists)}
  {
    if ${Pawn[Name, ${DTgtDeaggro}](exists)}
    {
      echo @#- VerifyID atom -#@ resetting DTgtDeaggroID ${DTgtDeaggroID} with new ID ${Pawn[Name, ${DTgtDeaggro}].ID}
      DTgtDeaggroID:Set[${Pawn[Name, ${DTgtDeaggro}].ID}]
    }
  }
  if ${FollowT.Length}>0 && !${Pawn[ID, ${FollowID}](exists)}
  {
    if ${Pawn[Name, ${FollowT}](exists)}
    {
      echo @#- VerifyID atom -#@ resetting FollowID ${FollowID} with new ID ${Pawn[Name, ${FollowT}].ID}
      FollowID:Set[${Pawn[Name, ${FollowT}].ID}]
    }
  }
  VerifyIDCheckT:Set[${Math.Calc[${Script.RunningTime}+30000]}]
}

atom VG_onAddInventoryItem(string ItemName, int ItemID, int Level, string Type1, string Keyword1, string Keyword2, string MiscDescription)
{
  echo Inventory changed with ${ItemName}
  NonCmbt.InvChanged:Set[TRUE]  
}

atom VG_onAlertText(string aText, int aType)
{
  ;echo --- alert text ${aText}
  if ${aText.Find[silence]} || ${aText.Find[Silence]}
  {
    ;echo --- WARNING: PLAYER SILENCED --- ${a.Text}
    Combat[parent].silenceCastT:Set[${Math.Calc[${Script.RunningTime}+1000]}]
    NonCmbt[parent].silenceCastT:Set[${Math.Calc[${Script.RunningTime}+1000]}]
  }
  ;if ${aText.Find[invalid target]} || ${aText.Find[Invalid target]}
  ;  VGExecute /cleartargets
}

atom VG_OnIncomingText(string aText, string ChannelNumber, string ChannelName)
{
	;echo --- incomingtext: ${aText}
  if ( ${aText.Find[becomes FURIOUS]} || ${aText.Find[Now I'm FURIOUS!]} ) && ${aText.Find[${Me.Target.Name}]}
  {
    Combat[parent].tgtFurious:Set[TRUE]
    Combat[parent].tgtFuriousT:Set[${Script.RunningTime}]
  }
	elseif ${aText.Find[no longer FURIOUS]} && ${aText.Find[${Me.Target.Name}]}
    Combat[parent].tgtFurious:Set[FALSE]
  if ${aText.Find[Bane of Heaven]} && ${aText.Find[too powerful]}
  {
    if !${Combat[parent].tgtTooPowerful}
      Combat[parent].tgtTooPowerful:Set[TRUE]
  }
  if ${aText.Find[nothing on it for you]} && ( ${doLoot} || ${doLootOnly} ) && ${Me.Target(exists)}
    LavishSettings[LootList]:AddSetting[${Me.Target.ID}, ${Me.Target.ID}]
  if ${aText.Find[cannot harvest it again]} && ( ${doResources} || ${doHarvest} )
  {
    LavishSettings[HarvestList]:AddSetting[${Me.Target.ID}, ${Me.Target.ID}]
    VGExecute /cleartargets
  }
  if ${aText.Find[do not have enough skill to begin harvesting]} && ( ${doResources} || ${doHarvest} )
  {
    LavishSettings[HarvestList]:AddSetting[${Me.Target.ID}, ${Me.Target.ID}]
    VGExecute /cleartargets
  }
  if ${aText.Find[###@@@]}
  {
    if ${aText.Find[ASSIST]}
      Combat:ForceAssist
    elseif ${aText.Find[BREAK]} && !${Paused}
      UIElement[ezvg].FindUsableChild[Run Button,button]:LeftClick
    elseif ${aText.Find[STARTED]} && ${Paused}
      UIElement[ezvg].FindUsableChild[Run Button,button]:LeftClick
    elseif ${aText.Find[LOG OUT]}
    {
      if !${Paused}
        UIElement[ezvg].FindUsableChild[Run Button,button]:LeftClick
      VGExecute /camp
    }
  }
}

atom VG_OnIncomingCombatText(string aText, int aType)
{
  ;echo --- combat text type ${aType} --- ${aText}
  variable string abilNm
  if (${aType} == 28 || ${aType} == 26) && ${aText.Find[${Me.Target.Name}]} && (${aText.Find[heal]} || ${aText.Find[absorb]})
  {
    abilNm:Set[${aText.Token[2,">"].Token[1,"<"]}]
    if ${Me.Class.NotEqual[Necromancer]} && ( !${abilNm.Find[Life]} || !${abilNm.Find[Devouring]} )
    {
      ;echo --- ezvg --- this mob ${Me.Target.Name} heals to ${abilNm} from school ${Me.Ability[${abilNm}].School}
      if ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Physical]} || ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Melee]}
      {
        Combat[parent].tgtNoPhy:Set[TRUE]
        call mobHealsTo "PHY"
      }
      elseif ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Spiritual]}
      {
        Combat[parent].tgtNoSpi:Set[TRUE]
        call mobHealsTo "SPI"
      }
      elseif ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Cold]}
      {
        Combat[parent].tgtNoCld:Set[TRUE]
        call mobHealsTo "CLD"
      }
      elseif ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Fire]}
      {
        Combat[parent].tgtNoFir:Set[TRUE]
        call mobHealsTo "FIR"
      }
      elseif ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Arcane]}
      {
        Combat[parent].tgtNoArc:Set[TRUE]
        call mobHealsTo "ARC"
      }
      elseif ${Me.Ability[${abilNm}].School.Token[1, " "].Equal[Mental]}
      {
        Combat[parent].tgtNoMen:Set[TRUE]
        callmobHealsTo "MEN"
      }
    } 
  }
  if ${aText.Find[Bane of Heaven]} && ${aText.Find[too powerful]}
  {
    if !${Combat[parent].tgtTooPowerful}
      Combat[parent].tgtTooPowerful:Set[TRUE]
  }
}

function mobHealsTo(string Line)
{
  if !${LavishSettings[MobDict].FindSet[${Me.Target.Name}](exists)}
    LavishSettings[MobDict]:AddSet[${Me.Target.Name}]
  if ${LavishSettings[MobDict].FindSet[${Me.Target.Name}].FindSetting[HealsTo](exists)}
    LavishSettings[MobDict].FindSet[${Me.Target.Name}].FindSetting[HealsTo]:Remove
  LavishSettings[MobDict].FindSet[${Me.Target.Name}]:AddSetting[HealsTo, ${Line}]
  call xmlSave
  call xmlLoad
  relay "all other" Scrtip[ezvg]:ExecuteAtom[EZVGListAmends,"HealsTo","${Me.Target.Name}","${Line}"]
}

atom VG_onPawnStatusChange(string ChangeType, int64 PawnID, string PawnName)
{
  echo --- VG_onPawnStatusChange --- ${ChangeType} ${PawnID} ${PawnName}
  if ${ChangeType.Equal[NowDead]} && ${PawnID}==${Me.Target.ID}
  {
    Combat:ResetTgt
    if ${doLoot}
      Me.Target:Loot
    if ${LavishSettings[Timers].FindSet[${PawnID}](exists)}
      LavishSettings[Timers].FindSet[${PawnID}]:Remove
  }
  elseif ${ChangeType.Equal[NowAlive]}
  {
    if ${LavishSettings[Timers].FindSet[${PawnID}](exists)}
      LavishSettings[Timers].FindSet[${PawnID}]:Remove
  }
}
